//
//  LZCouponPresenter.m
//  laziz_Consumer
//
//  Created by xbnzd on 16/11/19.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZCouponPresenter.h"
#import "NetworkServiceManager.h"
#import "LZGrabCouponAndFightGroupsDetialModel.h"
#import "MJExtension.h"
#import "LZUserModel.h"

extern NSString *customer;

static NSString *kCouponOrGroupsDetailUrl = @"/clientsalesPromotion/promotionInfo";    //优惠券或者拼团详情
static NSString *kMessageCenterTypeCouponOrGroupsDetailUrl = @"/clientsalesPromotion/promotionInfoBycouponId";    //根据抢卷或拼团id获取优惠详细信息 根据抢卷或拼团id获取优惠详细信息（type 1 优惠卷，type 2 团购）
static NSString *kGrabCouponUrl = @"/clientcustomerCoupons/add";     //抢得优惠券
static NSString *kUseCouponUrl = @"/clientcustomerCoupons/update";   //使用优惠券

@implementation LZCouponPresenter

/**
 抢券或者拼团详情
 
 @param videoId <#videoId description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getGrabCouponOrFightGroupsDetailWithVideoID:(NSString *)videoId ResultSuccess:(SuccessBlock)successBlock ResultFailed:(FailedBlock)failedBlock
{
    NSString *str = [NSString stringWithFormat:@"%@%@?id=%@",customer,kCouponOrGroupsDetailUrl,videoId];
    
    [[NetworkServiceManager manager] executGetTask:str parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        
        NSDictionary *dic = (NSDictionary *)resultData;
        if ([dic[@"statusCode"] integerValue] == kCodeSuccesss) {
            LZGrabCouponAndFightGroupsDetialModel *m = [LZGrabCouponAndFightGroupsDetialModel mj_objectWithKeyValues:dic[@"data"]];
            
            if (successBlock) {
                successBlock(m);
            }
        }else{
            NSString *str = dic[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}


/**
 根据抢卷或拼团id获取优惠详细信息 根据抢卷或拼团id获取优惠详细信息（type 1 优惠卷，type 2 团购）

 @param videoId <#videoId description#>
 @param type <#type description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)getMessageCenterTypeGrabCouponOrFightGroupsDetailWithVideoID:(NSString *)videoId andType:(NSString *)type ResultSuccess:(SuccessBlock)successBlock ResultFailed:(FailedBlock)failedBlock
{
    NSString *str = [NSString stringWithFormat:@"%@%@?id=%@&type=%@",customer,kMessageCenterTypeCouponOrGroupsDetailUrl,videoId,type];
    
    [[NetworkServiceManager manager] executGetTask:str parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        
        NSDictionary *dic = (NSDictionary *)resultData;
        if ([dic[@"statusCode"] integerValue] == kCodeSuccesss) {
            LZGrabCouponAndFightGroupsDetialModel *m = [LZGrabCouponAndFightGroupsDetialModel mj_objectWithKeyValues:dic[@"data"]];
            
            if (successBlock) {
                successBlock(m);
            }
        }else{
            NSString *str = dic[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}

/**
 抢券接口

 @param sellerId <#sellerId description#>
 @param promotionId <#promotionId description#>
 @param restaurantName <#restaurantName description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)grabCouponWithSellerID:(NSString *)sellerId PromotionID:(NSString *)promotionId RestaurantName:(NSString *)restaurantName ResultSuccess:(void (^)(void))successBlock ResultFailed:(FailedBlock)failedBlock
{
    
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kGrabCouponUrl];
    NSDictionary *dic = @{
                          @"sellerId":sellerId,
                          @"promotionId":promotionId,
                          @"restaurantName" : restaurantName,
                          @"token":[LZUserModel Instance].userToken
                          };
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        NSDictionary *dic = (NSDictionary *)resultData;
        if ([dic[@"statusCode"] integerValue] == kCodeSuccesss) {
            if (successBlock) {
                successBlock();
            }
        }else{
            NSString *str = dic[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}


/**
 使用优惠券
 
 @param couponId <#couponId description#>
 @param promotionId <#promotionId description#>
 @param successBlock <#successBlock description#>
 @param failedBlock <#failedBlock description#>
 */
+ (void)useCouponWithCouponID:(NSString *)couponId PromotionID:(NSString *)promotionId ResultSuccess:(void (^)(void))successBlock ResultFailed:(FailedBlock)failedBlock
{
    if ([LZUserModel Instance].userToken.length == 0) {
        [LZBasePresenter pushLoginViewController];
        if (failedBlock) {
            failedBlock(@"用户未登录,请登录后操作");
        }
        
        return;
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",customer,kUseCouponUrl];
    NSDictionary *dic = @{
                          @"id":couponId,
                          @"promotionId":promotionId,
                          @"token":[LZUserModel Instance].userToken
                          };
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        
        NSDictionary *dic = (NSDictionary *)resultData;
        if ([dic[@"statusCode"] integerValue] == kCodeSuccesss) {
            if (successBlock) {
                successBlock();
            }
        }else{
            NSString *str = dic[@"statusCode"];
            [LZBasePresenter checkAuthorStatus:str];
            if (failedBlock) {
                failedBlock(str);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failedBlock) {
            failedBlock(@"网络错误,请稍后再试");
        }
    }];
}

@end
